
DEFINE VARIABLE rate_avg AS DECIMAL FORMAT ">>>9.9<<<".
DEFINE VARIABLE cost_avg  AS DECIMAL FORMAT ">>>>>9.9<<<".
DEFINE VARIABLE qty  AS DECIMAL FORMAT ">>>>>>>9.9<<<".
DEFINE VARIABLE vend AS CHARACTER.
DEFINE VARIABLE nums AS INTEGER.

FOR EACH ptp_det WHERE can-do("P,M",ptp_pm_code) AND ptp_part BEGINS "914" EXCLUSIVE,
    EACH pt_mstr WHERE pt_part = ptp_part AND pt_part_type <> "RM-C" AND pt_part_type <> "FG-C" NO-LOCK:
    
    RUN getTaxRate(INPUT "RMB", INPUT "", INPUT TODAY - 90, INPUT TODAY, INPUT ptp_part, 
                   OUTPUT rate_avg, OUTPUT cost_avg, OUTPUT qty, OUTPUT vend, OUTPUT nums).
    
    IF /*abs(rate_avg - ptp__dec02) > 0.000001 AND */ rate_avg <> ? THEN DO:
       FIND FIRST ad_mstr WHERE ad_addr = vend NO-LOCK NO-ERROR.
       
/*        IF nums = 1 AND STRING (rate_avg * 100, "99") = substring(ad_tax_usage,2,3)  THEN
          ASSIGN ptp__dec02 = rate_avg.
*/

       DISP ptp_site ptp_part ptp_pm_code 
            rate_avg cost_avg qty
            ad_tax_usage WHEN(AVAILABLE ad_mstr) nums
            ptp__dec02 pt_desc1 pt_status pt_part_type
       WITH STREAM-IO WIDTH 200 .

    END.
END.


PROCEDURE getTaxRate.

    DEFINE INPUT PARAMETER base_curr  AS CHARACTER.
    DEFINE INPUT PARAMETER site    AS CHARACTER.
    DEFINE INPUT PARAMETER s_date  AS DATE.
    DEFINE INPUT PARAMETER e_date  AS DATE.
    DEFINE INPUT PARAMETER part    AS CHARACTER.
    DEFINE OUTPUT PARAMETER rate_avg AS DECIMAL.
    DEFINE OUTPUT PARAMETER cost_avg AS DECIMAL.
    DEFINE OUTPUT PARAMETER qty_tot  AS DECIMAL.
    DEFINE OUTPUT PARAMETER vend_max AS CHARACTER.
    DEFINE OUTPUT PARAMETER vend_num AS DECIMAL.
    

    DEFINE VARIABLE um_rate AS DECIMAL.
    DEFINE VARIABLE exc_rate AS DECIMAL.
    DEFINE VARIABLE qty_chg AS DECIMAL.
    DEFINE VARIABLE cst_tot AS DECIMAL.
    DEFINE VARIABLE qty_max AS DECIMAL.
    DEFINE VARIABLE tax_amt_in AS DECIMAL.
    DEFINE VARIABLE tax_amt_out AS DECIMAL.
    DEFINE VARIABLE tax_amt AS DECIMAL.
    DEFINE VARIABLE tax_rate AS DECIMAL.
    
    /*compute the average cost*/
    qty_max = 0.
    vend_max = "".
    vend_num = 0.
    tax_amt_in = 0.
    tax_amt_out = 0.
    tax_amt = 0.
    tax_rate = 0.
    qty_tot = 0.
    cst_tot = 0.
        
    FOR EACH tr_hist WHERE tr_type = 'rct-po' AND tr_part = part
                       AND (s_date <= tr_date AND tr_date <= e_date) 
                       AND tr_site BEGINS site 
                       AND tr_addr <> "S15032" NO-LOCK /*Filter the vend S13052 */
        BREAK BY tr_part BY tr_addr:

        FIND FIRST pt_mstr WHERE pt_part = part NO-LOCK NO-ERROR.

        RUN getItemConv(INPUT tr_part,INPUT pt_um, INPUT tr_um, OUTPUT um_rate).

        IF base_curr = tr_curr THEN
           exc_rate = 1.0.
        ELSE DO:
           RUN getCurrRate(INPUT base_curr, INPUT tr_curr, INPUT s_date, INPUT e_date, OUTPUT exc_rate).
           exc_rate = exc_rate * 1.17. /*if the currency is not base so the price is not tax_in */
        END.

        IF FIRST-OF (tr_addr) THEN 
           qty_chg = 0.
        
        /*accept the receiver in 500 RW03 warehouse N-N-N  and  m/s type could be considered.*/
        IF CAN-DO ("M,S",tr_ship_type) OR tr_status = "N-N-N" THEN DO: 
           qty_chg = qty_chg + tr_qty_loc * um_rate.
        END.
        ELSE DO:
           qty_chg = qty_chg + tr_qty_chg * um_rate.
        END.
        
        IF LAST-OF (tr_addr) THEN DO:
           vend_num = vend_num + 1.
           IF qty_chg > qty_max THEN DO:
              qty_max = qty_chg.
              vend_max = tr_addr.
           END.
        END.

        /*compute the average tax rate*/
        FIND FIRST pod_det WHERE pod_nbr = tr_nbr AND pod_line = tr_line NO-LOCK NO-ERROR.
        IF AVAILABLE pod_det THEN DO:
           IF base_curr = tr_curr THEN
              tax_rate = DECIMAL (SUBSTRING(pod_tax_usage,2,3)) / 100.
           ELSE 
              tax_rate = 0.17.
        END.

        /*accept the receiver in 500 RW03 warehouse N-N-N*/
        IF CAN-DO ("M,S",tr_ship_type) OR tr_status = "N-N-N" THEN DO:
            qty_tot = qty_tot + tr_qty_loc * um_rate.
            cst_tot = cst_tot + tr_qty_loc * um_rate * tr_price * exc_rate.
            tax_amt = tax_amt + ((tr_qty_loc * um_rate * tr_price * exc_rate) / ( 1 + tax_rate)) * tax_rate.
        END.
        ELSE DO:
            qty_tot = qty_tot + tr_qty_chg * um_rate.
            cst_tot = cst_tot + tr_qty_chg * um_rate * tr_price * exc_rate.
            tax_amt = tax_amt + ((tr_qty_chg * um_rate * tr_price * exc_rate) / ( 1 + tax_rate)) * tax_rate.
        END.
/*       
DISP tr_part FORMAT "X(12)"
     tr_nbr FORMAT "X(10)"
     tax_rate tax_amt cst_tot
     qty_tot FORMAT ">>>>>>>9.9<<<"
     pod_tax_in pod_tax_usage tr_curr exc_rate tr_addr
     tax_amt / (cst_tot - tax_amt) FORMAT ">>9.9<<"
WITH width 180.
*/
/*         FOR EACH tx2d_det NO-LOCK WHERE tx2d_ref = tr_lot AND tx2d_nbr = tr_nbr AND tx2d_line = tr_line ,    */
/*             EACH prh_hist NO-LOCK WHERE tx2d_ref = prh_receiver AND tx2d_nbr = prh_nbr                       */
/*                                     AND tx2d_line = prh_line AND prh_part = tr_part:                         */
/*                                                                                                              */
/*             IF base_curr = tr_curr THEN                                                                      */
/*                exc_rate = 1.0.                                                                               */
/*             ELSE DO:                                                                                         */
/*                RUN getCurrRate(INPUT base_curr, INPUT tr_curr, INPUT s_date, INPUT e_date, OUTPUT exc_rate). */
/*                tax_amt_in = tr_qty_chg * um_rate * tr_price * exc_rate * 0.17.                               */
/*             END.                                                                                             */
/*                                                                                                              */
/*             IF tx2d_tax_in THEN                                                                              */
/*                tax_amt_in = tax_amt_in + tx2d_tax_amt * exc_rate.                                            */
/*             ELSE                                                                                             */
/*                tax_amt_out = tax_amt_out + tx2d_tax_amt * exc_rate.                                          */
/*         END.                                                                                                 */
    END.

    IF qty_tot <> 0 THEN
       ASSIGN cost_avg = cst_tot / qty_tot.
    ELSE 
       ASSIGN cost_avg = ?.

    IF cost_avg <> ?  THEN 
       ASSIGN rate_avg  = (tax_amt) / (cst_tot - tax_amt).
    ELSE
       ASSIGN rate_avg  = ?.
    
END PROCEDURE.

PROCEDURE getCurrRate.
    DEFINE INPUT PARAMETER base_curr  AS CHARACTER.
    DEFINE INPUT PARAMETER targ_curr  AS CHARACTER.
    DEFINE INPUT PARAMETER s_date AS DATE.
    DEFINE INPUT PARAMETER e_date AS DATE.
    DEFINE OUTPUT PARAMETER exc_rate AS DECIMAL.
    
    FOR EACH exr_rate WHERE (exr_start_date <= e_date AND e_date <= exr_end_date)  OR e_date = ? NO-LOCK:

        IF exr_curr1 = base_curr AND exr_curr2 = targ_curr THEN DO:
           exc_rate = exr_rate.
           RETURN.
        END.
        ELSE IF exr_curr2 = base_curr AND exr_curr1 = targ_curr THEN DO:
           exc_rate = exr_rate2.
           RETURN.
        END.
    END.

END PROCEDURE.

PROCEDURE getItemConv.
    DEFINE INPUT  PARAMETER part AS CHARACTER.
    DEFINE INPUT  PARAMETER um1 AS CHARACTER.   /*stock base unit */
    DEFINE INPUT  PARAMETER um2 AS CHARACTER.   /*conver unit*/
    DEFINE OUTPUT PARAMETER rate AS DECIMAL.

    FIND FIRST um_mstr NO-LOCK WHERE um_part = part AND um_um = um1 AND um_alt_um = um2 NO-ERROR .
    IF AVAILABLE um_mstr THEN
       rate = um_conv .
    ELSE DO:
       FIND FIRST um_mstr NO-LOCK WHERE um_um = um1 AND um_alt_um = um2 NO-ERROR .
       IF AVAILABLE um_mstr THEN
          rate = um_conv .
       ELSE 
          rate = 1 .
    END.
END PROCEDURE.



